﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

using JavaSubstitutes;

namespace Sonar
{
	public class SonarSoundEngine
	{
		public SonarSoundEngine(int p)
		{
			// TODO: Complete member initialization
		}

		public SonarSoundEngine()
		{
			// TODO: Complete member initialization
		}


		public void setListener(SoundListener soundListener)
		{
			//don't care
			//throw new NotImplementedException();
		}

		//public void play(SonarSample samples, FixedSoundSource fixedSoundSource, int p, int p_2, int p_3)
		public void play(SonarSample sample, object source, int p, int p_2, int p_3)
		{
			sample.Sound.Position = new TimeSpan();
			sample.Sound.Play();
		}

		public SonarSample loadSample(GraphicsConfiguration gc, string sampleName)
		{
			return loadSample(gc, sampleName, false);
		}

		public SonarSample loadSample(GraphicsConfiguration gc, string sampleName, bool loop)
		{
			SonarSample sample = new SonarSample();
			sample.Sound = new MediaElement() { AutoPlay=false, Name=sampleName }; //the docs say that you need to give it a name on the phone....
			sample.Sound.MediaOpened += new RoutedEventHandler(Sound_MediaOpened);
			sample.Sound.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(Sound_MediaFailed);
			if(loop) sample.Sound.MediaEnded += Sound_MediaEnded; //loop
			sample.Sound.Source = new Uri(sampleName, UriKind.Relative);
			gc.LayoutRoot.Children.Add(sample.Sound);

			return sample;
		}

		void Sound_MediaOpened(object sender, RoutedEventArgs e)
		{
			//Debug.WriteLine("sound loaded:" + ((MediaElement)sender).Source.ToString() );
		}

		public static void Sound_MediaEnded(object sender, RoutedEventArgs e)
		{
			//loop
			((MediaElement)sender).Position = new TimeSpan();
			((MediaElement)sender).Play();
		}


		void Sound_MediaFailed(object sender, ExceptionRoutedEventArgs e)
		{
			Debug.WriteLine("Couldn't load sample: " + ((MediaElement)sender).Source.AbsoluteUri);
		}

		//public void play(SonarSample sonarSample, global::Sprites.Shell globalSpritesShell, int p, int p_2, int p_3)
		//{
		//    throw new NotImplementedException();
		//}

		//public void play(SonarSample sonarSample, global::Sprites.Shell globalSpritesShell, int p, int p_2, int p_3)
		//{
		//    throw new NotImplementedException();
		//}

		//public void clientTick(double alpha)
		//{
		//    throw new NotImplementedException();
		//}
	}
}
